/*
*  Copyright 2019-2020 Zheng Jie
*
*  Licensed under the Apache License, Version 2.0 (the "License");
*  you may not use this file except in compliance with the License.
*  You may obtain a copy of the License at
*
*  http://www.apache.org/licenses/LICENSE-2.0
*
*  Unless required by applicable law or agreed to in writing, software
*  distributed under the License is distributed on an "AS IS" BASIS,
*  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
*  See the License for the specific language governing permissions and
*  limitations under the License.
*/
package me.peng.service.impl;

import me.peng.domain.RecyclerFind;
import me.peng.domain.config.SysResult;
import me.peng.domain.mapper.FindMapper;
import me.peng.utils.ValidationUtil;
import me.peng.utils.FileUtil;
import lombok.RequiredArgsConstructor;
import me.peng.repository.RecyclerFindRepository;
import me.peng.service.RecyclerFindService;
import me.peng.service.dto.RecyclerFindDto;
import me.peng.service.dto.RecyclerFindQueryCriteria;
import me.peng.service.mapstruct.RecyclerFindMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import me.peng.utils.PageUtil;
import me.peng.utils.QueryHelp;

import java.util.*;
import java.io.IOException;
import javax.servlet.http.HttpServletResponse;

/**
* @website https://el-admin.vip
* @description 服务实现
* @author pengjianye
* @date 2021-10-13
**/
@Service
@RequiredArgsConstructor
public class RecyclerFindServiceImpl implements RecyclerFindService {

    private final RecyclerFindRepository recyclerFindRepository;
    private final RecyclerFindMapper recyclerFindMapper;

    @Override
    public Map<String,Object> queryAll(RecyclerFindQueryCriteria criteria, Pageable pageable){
        Page<RecyclerFind> page = recyclerFindRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder),pageable);
        return PageUtil.toPage(page.map(recyclerFindMapper::toDto));
    }

    @Override
    public List<RecyclerFindDto> queryAll(RecyclerFindQueryCriteria criteria){
        return recyclerFindMapper.toDto(recyclerFindRepository.findAll((root, criteriaQuery, criteriaBuilder) -> QueryHelp.getPredicate(root,criteria,criteriaBuilder)));
    }

    @Override
    @Transactional
    public RecyclerFindDto findById(Integer id) {
        RecyclerFind recyclerFind = recyclerFindRepository.findById(id).orElseGet(RecyclerFind::new);
        ValidationUtil.isNull(recyclerFind.getId(),"RecyclerFind","id",id);
        return recyclerFindMapper.toDto(recyclerFind);
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public RecyclerFindDto create(RecyclerFind resources) {
        return recyclerFindMapper.toDto(recyclerFindRepository.save(resources));
    }

    @Override
    @Transactional(rollbackFor = Exception.class)
    public void update(RecyclerFind resources) {
        RecyclerFind recyclerFind = recyclerFindRepository.findById(resources.getId()).orElseGet(RecyclerFind::new);
        ValidationUtil.isNull( recyclerFind.getId(),"RecyclerFind","id",resources.getId());
        recyclerFind.copy(resources);
        recyclerFindRepository.save(recyclerFind);
    }

    @Override
    public void deleteAll(Integer[] ids) {
        for (Integer id : ids) {
            Optional<RecyclerFind> resul=recyclerFindRepository.findById(id);

            if (resul.isPresent()&&resul.get().getImages()!=null){
                //删除操作
                String[] strings=resul.get().getImages().split(",");
                for (String s:strings){
                    FileServiceImpl.deleteFileData(s);
                }
            }
            recyclerFindRepository.deleteById(id);
        }
    }

    @Override
    public void download(List<RecyclerFindDto> all, HttpServletResponse response) throws IOException {
        List<Map<String, Object>> list = new ArrayList<>();
        for (RecyclerFindDto recyclerFind : all) {
            Map<String,Object> map = new LinkedHashMap<>();
            map.put("名称", recyclerFind.getFname());
            map.put("标签", recyclerFind.getLabel());
            map.put("签名", recyclerFind.getDecl());
            map.put("发布内容", recyclerFind.getContent());
            map.put("图片资源", recyclerFind.getImages());
            map.put("发布时间", recyclerFind.getCreateDate());
            map.put("点赞数", recyclerFind.getThubsnumber());
            map.put("评论数", recyclerFind.getCommentnumber());
            list.add(map);
        }
        FileUtil.downloadExcel(list, response);
    }
    @Autowired
    FindMapper findMapper;

    @Override
    public SysResult putFindInfo(RecyclerFind recyclerFind) {
        int update = findMapper.updateById(recyclerFind);
        return SysResult.success(update);
    }


}
